package com.beamfield.controller.manage.auth;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.AdminInfo;
import com.beamfield.modelVo.AdminInfoVo;
import com.beamfield.modelVo.OperatorVo;
import com.beamfield.service.manage.auth.AdminInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户管理
 */
@Controller
@RequestMapping(value = "/cms/auth/admininfo")
public class AdminController extends BaseController {

    private final AdminInfoService adminInfoService;

    @Autowired
    public AdminController(AdminInfoService adminInfoService) {
        this.adminInfoService = adminInfoService;
    }

    @RequestMapping(value = "/admin_info")
    public ModelAndView admin_info(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/add")
    public ModelAndView add(){
        return new ModelAndView();
    }

    /**
     * 获取后台用户数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/readAdminInfoData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readAdminInfoData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String name = request.getParameter("name");
        List<AdminInfoVo> adminInfoVoList = adminInfoService.getAdminInfoData(offset, limit, beamId, name);
        long total = adminInfoService.countByBeamId(beamId, name);
        return new PageResult(total, Long.parseLong(offset), adminInfoVoList);
    }

    /**
     * 获取系统角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRole",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getRole(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String type = request.getParameter("type");

        List<Map<String, String>> role = adminInfoService.getRole(beamId, type);
        return new ApiResult(role);
    }

    /**
     * 获取系统所有人员信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readOperatorData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readOperatorData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String type = request.getParameter("type");
        String name = request.getParameter("name");
        if(StringUtils.isEmpty(type)){
            return null;
        }
        List<OperatorVo> operatorVoList = adminInfoService.getOperatorData(offset, limit, beamId, type, name);
        long total = adminInfoService.countOperatorData(beamId, type, name);
        return new PageResult(total, Long.parseLong(offset), operatorVoList);
    }

    /**
     * 新增后台用户
     * @param adminInfo
     * @param request
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult save(AdminInfo adminInfo,HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        adminInfo.setOperator(userId);
        adminInfo.setBeam_id(beamId.toString());
        try {
            return adminInfoService.save(adminInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    @RequestMapping(value = "/modifyRole",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult modifyRole(HttpServletRequest request){
        String roleId = request.getParameter("roleId");
        String adminId = request.getParameter("adminId");
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(roleId) || StringUtils.isEmpty(adminId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return adminInfoService.modifyRole(beamId, userId, roleId, adminId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    @RequestMapping("/getLoginInfo")
    @ResponseBody
    public ApiResult getLoginInfo(HttpServletRequest request){
        Long userId = super.getUserId(request);
        return adminInfoService.getLoginInfo(userId);
    }
}
